#ifndef COMMANDSENDER_H
#define COMMANDSENDER_H

#include <QObject>
#include <QTimer>
#include <QMap>
#include "Command/BaseCmd.h"
#include "Protocol/BaseProtocol.h"
#include "DataType/PortStruct.h"
#include <QSharedPointer>


#include <QtNetwork>

class CommandSender : public QObject
{
    Q_OBJECT
public:
    explicit CommandSender(QObject *parent = 0);
    void registerProtocol(BaseProtocol* protocol);

    void sendCmd(PortStruct target,QSharedPointer<BaseCmd> sharedCmd);
    void cancelResend(quint16 cmdId);

    bool isEncrypt() const;
    void setIsEncrypt(bool isEncrypt);


    QNetworkAccessManager *manager;  //请求句柄
    QString allinfo;       //所有信息
    void postHttpRequest(QByteArray jsonData);
    void postHttpFileRequest(QString mac,QString filename);
    void postHttpIdentifi(QByteArray jsonData);
    void postHttpResult(QByteArray jsonData);
    void postHttpCreate(QByteArray jsonData);
    void postHttpLogin(QString usrname,QString pwd);

private:
    struct CmdStruct
    {
        PortStruct target;
        QSharedPointer<BaseCmd> sharedCmd;
    };
    QTimer *resendTimer;
    quint16 m_cmdId;
    QMap<quint16,CmdStruct> m_resendMap;
    bool m_isEncrypt;


    QList<BaseProtocol*> m_protocolList;
    void handle(PortStruct target,QSharedPointer<BaseCmd> sharedCmd);

    QString session_id;

    QString accssToken;

signals:
     void postDataSignal(PortStruct target,Package package);
    void sendDataSignal(PortStruct target,QByteArray data);
    void cmdTimeoutSignal(PortStruct target,QSharedPointer<BaseCmd> sharedCmd);
    void httpGetDataSignal(PortStruct target,Package package);
private slots:
    void timeoutSlot();

     void replyFinished(QNetworkReply *reply);

};

#endif // COMMANDDISPATCHER_H
